package View;

import Controller.POSSystem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PayingFrame extends JFrame{
    private JButton payButton = new JButton("提交支付");
    private JButton closeButton = new JButton("关闭钱柜");
    private JPanel listPanel = new JPanel();//上半部分显示购物列表
    private JPanel inputPanel = new JPanel();//下半部分显示结算输入
    private JTextArea listText = new JTextArea();
    private JLabel shouldPayLabel = new JLabel("应付额");
    private JLabel actualPayLabel = new JLabel("实付额");
    private JLabel balanceLabel = new JLabel("找零");
    private JTextField shouldPayText = new JTextField();
    private JTextField actualPayText = new JTextField();
    private JTextField balanceText = new JTextField();
    private static PayingFrame instance;

    private PayingFrame() {

        setTitle("结算窗口");
        setSize(330, 450);

        payButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                makePayment();
            }
        });
        closeButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                finishASale();
            }
        });

        listText.setEditable(false);
        listPanel.setLayout(new GridLayout());
        listPanel.add(listText);

        shouldPayText.setEditable(false);
        balanceText.setEditable(false);

        inputPanel.setLayout(new GridLayout(4,2));
        inputPanel.add(shouldPayLabel);
        inputPanel.add(shouldPayText);
        inputPanel.add(actualPayLabel);
        inputPanel.add(actualPayText);
        inputPanel.add(balanceLabel);
        inputPanel.add(balanceText);
        inputPanel.add(payButton);
        inputPanel.add(closeButton);

        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());
        pane.add(listPanel, BorderLayout.NORTH);
        pane.add(inputPanel, BorderLayout.SOUTH);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void finishASale() {
        POSSystem.getInstance().finishASale();
        JOptionPane.showMessageDialog(this, "正在打印单据");
        conceal();
    }

    private void makePayment() {
        balanceText.setText(String.valueOf(POSSystem.getInstance().
                makePayment(Double.parseDouble(actualPayText.getText()))));
    }

    public static PayingFrame getInstance() {
        if (instance == null) {
            instance = new PayingFrame();
        }
        return instance;
    }

    public void display() {
        if (!isVisible()) {
            listText.setText("商品名\t零售价\t数量\t金额");
            listText.append(POSSystem.getInstance().getSaleLineInfo());
            shouldPayText.setText(String.valueOf(POSSystem.getInstance().getSaleTotal()));
            actualPayText.setText("");
            balanceText.setText("");
            setVisible(true);
        }
    }

    public void conceal() {
        if (isVisible())
            setVisible(false);
    }
}
